const { execSync } = require("child_process");
const readline = require("readline");

// 创建readline接口
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

// 执行git add .
console.log("正在添加文件到暂存区...");
try {
  execSync("git add .", { stdio: "inherit" });
  console.log("文件添加成功！");
} catch (error) {
  console.error("git add 执行失败:", error.message);
  rl.close();
  process.exit(1);
}

// 提示用户输入提交信息
rl.question("请输入提交信息: ", (message) => {
  try {
    // 执行git commit
    console.log("正在提交更改...");
    execSync(`git commit -m "${message}"`, { stdio: "inherit" });
    console.log("提交成功！");

    // 执行git pull
    console.log("正在拉取远程更改...");
    execSync("git pull", { stdio: "inherit" });
    console.log("拉取成功！");

    // 执行git push
    console.log("正在推送到远程仓库...");
    execSync("git push", { stdio: "inherit" });
    console.log("推送成功！");

    console.log("✅ 所有操作完成！");
  } catch (error) {
    console.error("操作失败:", error.message);
  } finally {
    rl.close();
  }
});
